package io.renren.controller;

import io.renren.annotation.Login;
import io.renren.annotation.LoginUser;
import io.renren.common.utils.R;
import io.renren.entity.StudentEntity;
import io.renren.service.*;
import io.renren.util.ApiBaseAction;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;


/**
 * 科目表
 *
 * @author LANLING
 * @email lanling@gmail.comgg
 * @date 2018-10-23 10:47:09
 */
@RestController
@RequestMapping("/api/subject")
public class ApiSubjectController extends ApiBaseAction {
    @Autowired
    private SubjectService subjectService;
    @Autowired
    private StuSubService stuSubService;
    @Autowired
    private QuestionRecordService questionRecordService;
    @Autowired
    private QuestionService questionService;
    @Autowired
    private QuestionErrorService questionErrorService;

    /***
     * 获取科目列表
     * @param student 学员
     * @return R
     */
    @Login
    @PostMapping("getSubjectList")
    @ApiOperation("获取科目列表")
    public R getSubjectList(@LoginUser StudentEntity student) {
        List<Map<String, Object>> subjectList = subjectService.getSubjectList(student.getUserId());
        return R.ok(subjectList);
    }

    /***
     * 小程序
     * 获取科目列表
     * @param student 学员
     * @param subjectName 科目名称
     * @return
     */
    @Login
    @PostMapping("getXSubjectList")
    @ApiOperation("获取科目列表")
    public R getXSubjectList(@LoginUser StudentEntity student,
                             String subjectName) {
        List<Map<String, Object>> subjectList = subjectService.getXSubjectList(student, subjectName);
        return R.ok(subjectList);
    }




    /***
     * 获取练习题目信息
     * @param student 学员
     * @param subjectId 科目id
     * @param exercisesType 1顺序练习 2随机 3错题
     * @param sort 序号(只有顺序练习有)
     * @param state 是否查序号
     * @param type 针对错题练习 1为错题题目练习 2为收藏题目练习(错题练习一定要传type)
     * @return R
     */
    @Login
    @PostMapping("getQuestionList")
    @ApiOperation("获取顺序练习，随机练习，错题练习题目")
    public R getQuestionList(@LoginUser StudentEntity student,
                             @RequestParam("subjectId") Integer subjectId,
                             Integer exercisesType,
                             Integer sort,
                             Integer state,
                             Integer type) {
        return subjectService.getQuestionList(student, subjectId, exercisesType, sort, state, type);
    }


    /***
     * 清空收藏题目
     * @param student 学员
     * @param subjectId 科目id
     * @return R
     */
    @Login
    @PostMapping("resetCollectionQuestion")
    @ApiOperation("清空收藏题目")
    public R resetCollectionQuestion(@LoginUser StudentEntity student,
                                     @RequestParam("subjectId") Integer subjectId) {
        questionErrorService.resetCollectionQuestion(student.getUserId(), subjectId);
        return R.ok();
    }

    /***
     * 重置顺序练习
     * @param student 学员
     * @param subjectId 科目id
     * @return R
     */
    @Login
    @PostMapping("reset")
    @ApiOperation("重置顺序练习")
    public R reset(@LoginUser StudentEntity student,
                   @RequestParam("subjectId") Integer subjectId) {
        questionRecordService.reset(student.getUserId(), subjectId);
        return R.ok();
    }


    /***
     * 保存学员的答题记录
     * @param student 学员
     * @param questionId 题目id
     * @param exercisesType 练习类型，1表示顺序练习；2表示随机练习；3表示错题练习
     * @param studentAnswer 学员答案
     * @return R
     */
    @Login
    @PostMapping("recordAnswer")
    @ApiOperation("保存练习答案")
    public R recordAnswer(@LoginUser StudentEntity student,
                          @RequestParam("questionId") Integer questionId,
                          @RequestParam("exercisesType") Integer exercisesType,
                          @RequestParam("studentAnswer") String studentAnswer) {
        return subjectService.recordAnswer(student, questionId, exercisesType, studentAnswer);
    }


}
